package com.mywork.mvpkodeingithub.ui

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.mywork.mvpkodeingithub.R
import com.mywork.mvpkodeingithub.base.ui.activity.BaseMvpActivity
import com.mywork.mvpkodeingithub.data.response.Repo
import com.mywork.mvpkodeingithub.data.response.User
import com.mywork.mvpkodeingithub.presenter.MainPresenter
import com.mywork.mvpkodeingithub.repository.GithubRepository.Companion.PAGE_COUNT
import com.mywork.mvpkodeingithub.view.MainView
import com.orhanobut.logger.Logger
import com.paginate.Paginate
import kotlinx.android.synthetic.main.activity_main.*
import org.kodein.di.Copy
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
import org.kodein.di.android.retainedKodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.instance

class MainActivity : BaseMvpActivity<MainPresenter>(), MainView, KodeinAware, View.OnClickListener {

    private val usersAdapter: UsersAdapter = UsersAdapter()
    private var mIsLoading: Boolean = false
    override val kodein: Kodein by retainedKodein {
        extend(parentKodein, copy = Copy.All)
        bind() from instance(this@MainActivity)
    }

    //Inject
    override val mPresenter: MainPresenter by instance()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initViews()
    }

    private fun initViews() {
        mRepo.setOnClickListener(this)
        mRvUsers.adapter = usersAdapter
        mRvUsers.layoutManager = GridLayoutManager(this, 2)
        mSrlUsers.setOnRefreshListener {
            usersAdapter.resetData()
            mPresenter.getUsers(true)
        }
        val callbacks = object : Paginate.Callbacks {
            override fun onLoadMore() {
                mPresenter.getUsers(false)
            }

            override fun isLoading(): Boolean {
                return mIsLoading
            }

            override fun hasLoadedAllItems(): Boolean {
                return false
            }
        }
        Paginate.with(mRvUsers, callbacks)
            .setLoadingTriggerThreshold(PAGE_COUNT)
            .build()
    }

    override fun onGetUsers(result: List<User>, source: String) {
        val users: ArrayList<UserWithSource> = ArrayList<UserWithSource>()
        result.forEach { user ->
            users.add(UserWithSource(user, source))
        }

        usersAdapter.addData(users)
    }

    override fun onGetRepo(result: List<Repo>, source: String) {
        result.forEach { repo -> Logger.d(repo) }
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.mRepo -> {
                mPresenter.getRepos("defunkt")
            }


            else -> {
            }
        }
    }

    override fun showLoading() {
        mSrlUsers.isRefreshing = true
        mIsLoading = true
    }

    override fun hideLoading() {
        mSrlUsers.isRefreshing = false
        mIsLoading = false
    }
}
